/**基本物件派生类:线段
 * date： 2011 4 10
 * author: zzy
 **/
package cn.edu.dlut.WingGame.Object;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import cn.edu.dlut.WingGame.BaseType.WingColor;
import cn.edu.dlut.WingGame.Graphic.GLDevice;

public class WingLine extends WingObject {
	private float x1 = 0;
	private float y1 = 0;
	private float x2 = 100;
	private float y2 = 100;
	private float lineWidth = 1;
	private WingColor lineColor = new WingColor();

	public void setLinePos(float x1, float y1, float x2, float y2) {
		this.x1 = x1;
		this.y1 = y1;
		this.x2 = x2;
		this.y2 = y2;
	}

	public void setLineWidth(float lineWidth) {
		this.lineWidth = lineWidth;
	}

	public void setLineColor(WingColor lineColor2) {
		lineColor.alpha = lineColor2.alpha;
		lineColor.b = lineColor2.b;
		lineColor.r = lineColor2.r;
		lineColor.g = lineColor2.g;
	}

	public WingLine() {
	}

	public WingLine(float x1, float y1, float x2, float y2, float lineWidth,
			WingColor lineColor) {
		setLinePos(x1, y1, x2, y2);
		setLineWidth(lineWidth);
		setLineColor(lineColor);
	}

	public void drawLine() {
		ByteBuffer vbb = ByteBuffer.allocateDirect(16);
		vbb.order(ByteOrder.nativeOrder());
		FloatBuffer vertices = vbb.asFloatBuffer();

		vertices.put(x1);
		vertices.put(y1);
		vertices.put(x2);
		vertices.put(y2);
		vertices.position(0);
		if (lineColor.hasAlpha())
			GLDevice.enableBlend();
		GLDevice.getGL().glVertexPointer(2, GL10.GL_FLOAT, 0, vertices);
		GLDevice.getGL().glEnableClientState(GL10.GL_VERTEX_ARRAY);
		GLDevice.getGL().glLineWidth(lineWidth);
		GLDevice.getGL().glColor4f(lineColor.getRf(), lineColor.getGf(),
				lineColor.getBf(), lineColor.getAf());
		GLDevice.getGL().glDrawArrays(GL10.GL_LINES, 0, 2);
		GLDevice.getGL().glDisableClientState(GL10.GL_VERTEX_ARRAY);
		if (lineColor.hasAlpha())
			GLDevice.disableBlend();
	}

	@Override
	public void render() {
		// TODO Auto-generated method stub
		drawLine();
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
	}

}